<?php

namespace app\master\controller;

use think\Db;
use think\Exception;

class Contract extends Base
{
    //车辆状态
    public function contractlist()
    {
        $data['start_date'] = input('start_date');
        $data['end_date'] = input('end_date');
        $data['company_id'] = input('company_id');
        if (request()->isGet()) {
            $company = db('company')->where(["company_status"=>1,"pid"=>0])->field("company_id,company_name")->select();
            $this->assign('company', $company);
            $this->assign('data', $data);
            return $this->fetch('contractlist');
        }
        $where = [];
        if ($data['company_id']) $where['c.company_id'] = $data['company_id'];
//        if ($data['start_date'] && $data['end_date']) {
//            $where['c.contractcar_bigontime'] = ['between', [strtotime($data['start_date']), strtotime($data['end_date'])]];
//        }
        if ($data['start_date']) {
            $where['c.start_time'] = ['egt', strtotime($data['start_date'])];
        }
        if ($data['end_date']) {
            $where['c.end_time'] = ['elt', strtotime($data['end_date'])];
        }
//        $where = [
//            'co.company_status'=>1,
//        ];
        $page = input('page');
        $limit = input('limit');
        $list = model('Contract')->getcontractlist($where, $page, $limit);
        return $this->tableReturn(0, '查询成功', $list['count'], $list['data']);
    }
    //合同详情
    public function info(){
        $contract_id = input('contract_id');
        $content =  htmlspecialchars_decode(db('contract')->where("contract_id",$contract_id)->value('content'));
        $this->assign("content",$content);
        return $this->fetch();
    }
    //添加合同
    public function addcontract()
    {
        if (request()->isGet()) {
            $company = db("company")->where(["company_status"=>1,"pid"=>0])->select();
            $this->assign("company", $company);
            return $this->fetch("addcontract");
        }
        $data = input("post.");
        $admin_id = session('admin_id');
        $contract = [
            "company_id" => $data['company_id'],
            'start_time' => strtotime($data['start_time']),
            'end_time' => strtotime($data['end_time']),
            "content" => $data['content'],
            "admin_id" => $admin_id,

        ];
        $list = db('contract')->insert($contract);
        if ($list){
            $this->ajaxReturn(200, successMsg, url("Contract/contractlist"));
        }else{
            $this->ajaxReturn(603, errorMsg);
        }
    }
}